import { Order } from '../order/order-model.js';
var order = new Order();
Page({
  data: {
    radioItems: [
      { name: '微信支付', img: '../../imgs/wechat.png', value: '0', 'checked': true },
      { name: '余额支付', img: '../../imgs/wallet.png', value: '1', checked: false,money:0.00 }
    ],
    payMethod : 0,
    showInput:false,//支付密码输入框显示
  },
  onLoad: function (options) {
    //入口方式 1订单列表，2提交订单
    this.data.entry = options.entry ? options.entry : 1
    //订单信息
    this.data.id = options.order_id ? options.order_id : '';
    this.data.parent_sn = options.parent_sn ? options.parent_sn : ''
    if (this.data.id || this.data.parent_sn)
      this.getOrderPay();
  },
  //获取订单支付信息
  getOrderPay: function () {
    var that = this ;
    if (this.data.id){
      var args = {
        order_id: this.data.id
      }
    }
    if (this.data.parent_sn) {
      var args = {
        parent_sn: this.data.parent_sn
      }
    }
    wx.showLoading({
      title: '加载中',
    })
    order.getOrderPay(args, (data) => {
      wx.hideLoading();
      that.setData({
        item:data.item,
        'radioItems[1].money':data.item.user_money
      })
      var end = data.item.end_time.replace(/-/g, '/');
      var end_time = (new Date(end)).getTime() / 1000;
      that.data.second = parseInt(end_time - (new Date().getTime()) / 1000);
      if (that.data.second <= 0){
        that.setData({
          overtiem: true
        })
      }else{
        that.data.interval = setInterval(that.countDown, 1000);
      }
      
    })
  },
  //支付方式
  radioChange: function (e) {
    this.data.payMethod = e.detail.value;
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }
    this.setData({
      radioItems: radioItems
    });
  },
  //点击立即支付
  pay:function(){
    var that = this;
    //支付参数
    if (this.data.id) {
      var params = {
        order_id: this.data.id
      }
    }
    if (this.data.parent_sn) {
      var params = {
        parent_sn: this.data.parent_sn
      }
    }
    if (this.data.payMethod == 0){
      //微信支付
      this._execPay(params);
    } else if (this.data.payMethod == 1) {
      //余额支付
      //传参给设置支付密码/充值后做跳转回来
      if (this.data.id) {
        var type = 1;
        var id = this.data.id;
      }
      if (this.data.parent_sn) {
        var type = 2;
        var id = this.data.parent_sn;
      }
      if (parseFloat(this.data.radioItems[1].money) < parseFloat(this.data.item.pay_amount)){
        wx.showModal({
          title: '提示',
          content: '您的余额不足',
          confirmText: "去充值",
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../center/recharge?type=' + type + '&id=' + id
              });
            } else if (res.cancel) {
              console.log(1)
            }
          }
        })
        return false;
      }
      
      //没绑定手机号码，也没有设置支付密码
      if (this.data.item.set_mobile == 0 && this.data.item.set_pay_pwd == 0)
      {
        wx.showModal({
          title: '提示',
          content: '请先绑定您的手机并设置支付密码',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../member/binding?setPassword=1&type='+type+'&id='+id
              });
            } else if (res.cancel) {
              console.log(1)
            }
          }
        })
        return false;
      }
      //绑定了手机，但没有设置支付密码
      if (this.data.item.set_mobile == 1 && this.data.item.set_pay_pwd == 0){
        wx.showModal({
          title: '提示',
          content: '请先设置支付密码',
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../member/set-payment?type=' + type + '&id=' + id
              });
            } else if (res.cancel) {
              console.log(2)
            }
          }
        })
        return false;
      }
      //支付密码输入框
      this.setData({
        showInput : true
      });
    }
  },
  //微信支付
  _execPay: function (params) {
    var that = this;
    order.execPay(params, (data) => {
      if (data.status == 0) {
        wx.showToast({
          icon: 'none',
          duration: 2000,
          title: data.msg ? data.msg : '支付失败',
        })
        return false;
      }
      if (data.is_cash == 0) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/order/list?index=0',
              });
            }, 2000)
          }
        })
      } else if (data == 1 || data == 2) {
        wx.navigateTo({
          url: '/pages/order/list?index=0',
        });
      }
    });
  },
  //余额支付
  payConfirm: function () {
    var that = this;
    if (!this.data.pay_pwd){
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000,
      })
      return false;
    }
    //支付参数
    if (this.data.id) {
      var params = {
        order_id: this.data.id
      }
    }
    if (this.data.parent_sn) {
      var params = {
        parent_sn: this.data.parent_sn
      }
    }
    params.pay_pwd = this.data.pay_pwd;
    order.accountPay(params, (data) => {
      if (data.status == 1) {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000,
          success: function () {
            setTimeout(function () {
              wx.navigateTo({
                url: '/pages/order/list?index=0'
              });
            }, 2000)
          }
        })
      } else{
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    });
  },
  //关闭弹窗
  hideModal: function (e) {
    this.setData({
      showInput: false
    })
  },
  //获取输入的支付密码
  code: function (e) {
    this.data.pay_pwd = e.detail.value
  },
  //卸载页面
  onUnload: function () {
    clearInterval(this.data.interval);
    if(this.data.entry == 2){
      wx.navigateTo({
        url: '/pages/order/list?index=0&entry=2'
      });
    }
  },
  toSetPass:function () {
    //传参给设置支付密码/充值后做跳转回来
    if (this.data.id) {
      var type = 1;
      var id = this.data.id;
    }
    if (this.data.parent_sn) {
      var type = 2;
      var id = this.data.parent_sn;
    }
    wx.navigateTo({
      url: '../member/set-payment?type=' + type + '&id=' + id
    });
  },
  //支付倒计时
  countDown: function () {
    var second = this.data.second;
    this.data.second --;
    let countDownArr = {};
    if (this.data.second <= 0){
      clearInterval(this.data.interval);
      return false;
    }
    var obj = null;
    // 天数位
    var day = Math.floor(second / 3600 / 24);
    var dayStr = day.toString();
    if (dayStr.length == 1) dayStr = '0' + dayStr;

    // 小时位
    var hr = Math.floor((second - day * 3600 * 24) / 3600);
    var hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;

    // 分钟位
    var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
    var minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;

    // 秒位
    var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
    var secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;
    countDownArr = {
      day: dayStr,
      hou: hrStr,
      min: minStr,
      sec: secStr
    }
    this.setData({ pay_time: countDownArr})
  },
})